/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package queueing;

import entity.Wish;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import session.WishFacadeRemote;

/**
 *
 * @author Fabio
 */
@MessageDriven(mappedName = "jms/coda", activationConfig =  {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
    })
public class NotifyQueue implements MessageListener {
    @EJB
    private WishFacadeRemote wishFacade;
    
    public NotifyQueue() {
    }

    public void onMessage(Message message) {
        if(message instanceof TextMessage) {
            TextMessage msg = (TextMessage) message;
            try {
                Long id = Long.parseLong(msg.getText());
                Wish wish = wishFacade.find(id);
                wishFacade.remove(wish);
            } catch (JMSException ex) {
                Logger.getLogger(NotifyQueue.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}
